# -*- coding: utf-8 -*-
import logging

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from steam_data_collect.exceptions import RuleGetError
from steam_data_collect.items import BrainpopSteamDataCollectItem
from steam_data_collect.models import brainpop_rule_infos
loger=logging.getLogger(__file__)

class BrainpopSteamSpider(CrawlSpider):
    name = 'brainpop_steam'
    allowed_domains = ['brainpop.com']
    start_urls = ['https://educators.brainpop.com/stem-lesson-plans/']

    custom_settings = {'DEPTH_PRIORITY': 1,
                       'DEPTH_STATS_VERBOSE': True,
                       'USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
                       'COOKIES_ENABLED': False,
                       'DOWNLOAD_TIMEOUT': 60,
                       }

    rules = (
        Rule(LinkExtractor(restrict_xpaths=("//div[@class='read-more']/a",)), callback='parse_item', follow=False),
    )

    def __init__(self,*a,**kw):
        super(BrainpopSteamSpider,self).__init__(*a,**kw)
        self.rule_obj=brainpop_rule_infos.select().first()
        if None==self.rule_obj:
            loger.error(RuleGetError)
            raise RuleGetError

    def parse_item(self, response):
        loger.info(response.url)

        class_title=response.xpath(self.rule_obj.class_title).extract()
        class_desc = response.xpath(self.rule_obj.class_desc).extract()
        challenge_desc = response.xpath(self.rule_obj.challenge_desc).extract()
        objective = response.xpath(self.rule_obj.objective).extract()
        directions = response.xpath(self.rule_obj.directions).extract()
        materials = response.xpath(self.rule_obj.materials).extract()
        extended_content = response.xpath(self.rule_obj.extended_content).extract()

        item_obj = BrainpopSteamDataCollectItem()
        item_obj['url'] = response.url
        item_obj["class_title"] = class_title
        item_obj["class_desc"] = class_desc
        item_obj["class_img"] = []
        item_obj["challenge_title"] = []
        item_obj["challenge_desc"] = challenge_desc
        item_obj["objective"] = objective
        item_obj["background_information"] = []
        item_obj["science_skills"] = []
        item_obj["technology_skills"] = []
        item_obj["engineering_skills"] = []
        item_obj["arts_skills"] = []
        item_obj["math_skills"] = []
        item_obj["directions"] = directions
        item_obj["materials"] = materials
        item_obj["wrap_up"] = []
        item_obj["extended_content"] = extended_content
        item_obj["extended_picture"] = []
        item_obj["achievements_picture"] = []

        return item_obj
